﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Hotel.DichVu;

namespace Hotel.DichVu
{
    public partial class Them_DV : Form
    {
        public Them_DV()
        {
            InitializeComponent();
        }

        private SqlConnection con;
        private SqlDataAdapter da = new SqlDataAdapter();
        QL_dichvu quanly = new QL_dichvu();

        private void connect()
        {
            string cn = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void serverdata(string sql)
        {
            try
            {
                connect();
                SqlCommand command = new SqlCommand();
                command.Connection = con;
                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi cơ sở dữ liệu!!!", "Lỗi cơ sở dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tbxThemDichVuTenDV_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.Handled = !(((e.KeyChar >= 48) && (e.KeyChar <= 57)) || ((e.KeyChar >= 65) && (e.KeyChar <= 122)) || (e.KeyChar == 8) || (e.KeyChar == 32));
        }

        private void btnThemDichVuReset_Click(object sender, EventArgs e)
        {
            tbxThemDichVuMaDV.ResetText();
            tbxThemDichVuTenDV.ResetText();
            tbxThemDichVuDonGia.ResetText();
        }

        private void btnThemDichVuThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnThemDichVuThem_Click(object sender, EventArgs e)
        {
            if ((tbxThemDichVuMaDV.Text == "") || (tbxThemDichVuTenDV.Text == "") || (tbxThemDichVuDonGia.Text == ""))
                MessageBox.Show("Mời bạn nhập đầy đủ thông tin cho dịch vụ!!!", "Lỗi thiếu thông tin", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else if (!CheckTonTaiTenDichVu(tbxThemDichVuTenDV.Text) && (!CheckTonTaiMaDichVu(tbxThemDichVuMaDV.Text)))
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn lưu thông tin?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    try
                    {
                        String sql = "INSERT INTO [Hotel].[dbo].[KhoDichVu]"
                                        + " ([MaHang]"
                                        + " ,[TenHang]"
                                        + " ,[DonGia]"
                                        + " ,[SoLuong]"
                                        + " ,[DaXoa])"
                                    + " VALUES"
                                        + " ('" + tbxThemDichVuMaDV.Text + "'"
                                        + " ,N'" + tbxThemDichVuTenDV.Text + "'"
                                        + " ," + tbxThemDichVuDonGia.Text + ""
                                        + " ,0"
                                        + " ,0)";
                        //serverdata("INSERT INTO KhoDichVu(MaHang, TenHang,DonGia,SoLuong, DaXoa) VALUES ('" + tbxThemDichVuMaDV.Text + "','" + tbxThemDichVuTenDV.Text + "'," + tbxThemDichVuDonGia.Text + ",0, 0)");
                        serverdata(sql);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Lỗi cập nhật cơ sở dữ liệu", "Thông báo lỗi!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    //quanly.QL_dichvu_Load(sender, e);
                    //quanly.khoDichVuTableAdapter.Fill(quanly.hotelDataSet.KhoDichVu);
                    MessageBox.Show("Thêm dịch vụ thành công!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Dispose();
                }
                else if (dr == DialogResult.No)
                {
                    MessageBox.Show("Chưa thêm dịch vụ!", "Thông Báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            else MessageBox.Show("Tên hoặc mã dịch vụ này đã tồn tại. Mời bạn kiểm tra lại", "Lỗi!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        private void tbxThemDichVuDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !((e.KeyChar >= 48) && (e.KeyChar <= 57) || (e.KeyChar == 8));
        }

        private void tbxThemDichVuMaDV_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(((e.KeyChar >= 48) && (e.KeyChar <= 57))||((e.KeyChar >= 65) && (e.KeyChar <= 122)) ||(e.KeyChar == 8));
        }


        private Boolean CheckTonTaiTenDichVu(String tendichvu)
        {

            String sql = "select * from KhoDichVu where TenHang = N'" + tendichvu + "' AND DaXoa = 0 ";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            if (dt.Rows.Count > 0)
                return true;
            return false;

        }

        private Boolean CheckTonTaiMaDichVu(String madichvu)
        {

            String sql = "select * from KhoDichVu where MaHang = N'" + madichvu + "' AND DaXoa = 0 ";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            if (dt.Rows.Count > 0)
                return true;
            return false;

        }
    }
}
